const requireDirectory = require('require-directory');
const Router = require('koa-router');
const config = require('../config/config')

class InitManager {

  static initCore(app) {
    // 入口方法
    InitManager.app = app;
    InitManager.initLoadRouters();
    InitManager.env();
  }

  static initLoadRouters() {
    function loudModule(obj) {
      if (obj instanceof Router) {
        InitManager.app.use(obj.routes())
      }
    }
    const apiDirectory = `${process.cwd()}/server/app/api`
    requireDirectory(module, apiDirectory, {
      visit: loudModule
    })
  };

  static env() {
    Object.keys(config).forEach(k => {
      if (global[k]) throw new Error(`${k}-该变量在global中已有值`)
      global[k] = config[k]
    })
  }


}

module.exports = InitManager;